﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
namespace Common
{
    /// <summary>
    /// 方便获取或设置ViewState的值
    /// </summary>
    public static class ViewStateHelper
    {
        /// <summary>
        /// Sets the state of the view.
        /// </summary>
        /// <param name="viewState">State of the view.</param>
        /// <param name="value">The value.</param>
        /// <param name="name">The name.</param>
        public static void SetViewState(StateBag viewState, object value, string name)
        {
            viewState[name] = value;
        }

        /// <summary>
        /// Gets the state of the view.
        /// </summary>
        /// <param name="viewState">State of the view.</param>
        /// <param name="name">The name.</param>
        /// <returns></returns>
        public static object GetViewState(StateBag viewState, string name)
        {
            return viewState[name];
        }

        /// <summary>
        /// Gets the int.
        /// </summary>
        /// <param name="viewState">State of the view.</param>
        /// <param name="name">The name.</param>
        /// <returns></returns>
        public static int? GetInt(StateBag viewState, string name)
        {
            object o = GetViewState(viewState, name);
            int? tempInt = TypeHelper.GetInt(o);
            return tempInt;
        }

        /// <summary>
        /// Gets the int.
        /// </summary>
        /// <param name="viewState">State of the view.</param>
        /// <param name="name">The name.</param>
        /// <returns></returns>
        public static bool? GetBool(StateBag viewState, string name)
        {
            object o = GetViewState(viewState, name);
            bool? tempInt = TypeHelper.GetBool(o);
            return tempInt;
        }

        /// <summary>
        /// Gets the string.
        /// </summary>
        /// <param name="viewState">State of the view.</param>
        /// <param name="name">The name.</param>
        /// <returns></returns>
        public static string GetString(StateBag viewState, string name)
        {
            object o = GetViewState(viewState, name);
            string tempStr = TypeHelper.GetString(o);
            return tempStr;
        }
    }
}
